<?php

namespace App\Http\Controllers\Index;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class WebpageController extends Controller
{
    //前台首页
    public function index(){
        $phone=session('phone');
        // 轮播
        $banner=DB::table('carousel')->get();
        // 一、二、三级导航
        $arr1=DB::table('goodscategory')->where("rank",1)->get();
        $arr2=DB::table('goodscategory')->where("rank",2)->get();
        $arr3=DB::table('goodscategory')->where("rank",3)->get();
        // 商品
        foreach($arr1 as $i=>$gc){
            $garr[$i]=DB::table('goods')->where("keywords",'like',"%{$gc->classify}%")->limit(8)->get();
        }
        // 购物车商品数量
        $shopcart_count=DB::table('shopcart')->where('uid',session('uid'))->count();
    	return view('index/webpage/index',['phone'=>$phone,'banner'=>$banner,'arr1'=>$arr1,'arr2'=>$arr2,'arr3'=>$arr3,'garr'=>$garr,'shopcart_count'=>$shopcart_count]);
    }

    // 商品列表
    public function item_category(Request $request){
        $arr1=DB::table('goodscategory')->where("rank",1)->get();
        $arr2=DB::table('goodscategory')->where("rank",2)->get();
        $arr3=DB::table('goodscategory')->where("rank",3)->get();
        // 根据关键字搜索
        if($request->keywords){
            $goods=DB::table('goods')->where('keywords','like',"%{$request->keywords}%")->paginate(20);
        }else{
            $goods=DB::table('goods')->paginate(20);
        }
        if($request->cls){
            if($request->cls=='全部分类'){
                $goods=DB::table('goods')->paginate(20);
            }else{
                $clsid=DB::table('goodscategory')->where("classify",$request->cls)->value('id');
                $arr2=DB::table('goodscategory')->where("pid",$clsid)->get();
                $goods=DB::table('goods')->where('keywords','like',"%{$request->cls}%")->paginate(20);
            }
        }
        if($request->cls2){
            if($request->cls=='全部分类'){
                if($request->cls2=='全部'){
                    $goods=DB::table('goods')->paginate(20);
                }else{
                    $goods=DB::table('goods')->where('keywords','like',"%{$request->cls2}%")->paginate(20);
                }
            }else{
                if($request->cls2=='全部'){
                    $goods=DB::table('goods')->where('keywords','like',"%{$request->cls}%")->paginate(20);
                }else{
                    $goods=DB::table('goods')->where([['keywords','like',"%{$request->cls}%"],['keywords','like',"%{$request->cls2}%"]])->paginate(20);
                }
            }
            
        }
        // 热卖商品
        $hotgoods=DB::table('goods')->orderBy('sales','desc')->limit(5)->get();
        // 购物车商品数量
        $shopcart_count=DB::table('shopcart')->where('uid',session('uid'))->count();
    	return view('index/webpage/item_category',['arr1'=>$arr1,'arr2'=>$arr2,'arr3'=>$arr3,'goods'=>$goods,'shopcart_count'=>$shopcart_count,'hotgoods'=>$hotgoods]);
    }

    // 筛选商品
    public function changesize(Request $request){
        // return $request->all();
        // 商品规格
        $size=DB::table('goodssize')->where([['gid',$request->gid],['color',$request->color]])->orderBy('size')->get();
        return $size;
    }

    // 商品详情
    public function item_show(Request $request){
        // 浏览记录
        session_start();
        // session_destroy();
        if(isset($_SESSION['history'])){
            array_unshift($_SESSION['history'],$request->id);
            $_SESSION['history']=array_unique($_SESSION['history']);
        }else{
            $_SESSION['history']=[];
            $_SESSION['history'][]=$request->id;
        }
        // dd($_SESSION['history']);
        // 购物车商品数量
        $shopcart_count=DB::table('shopcart')->where('uid',session('uid'))->count();
        // 所点商品
        $piece=DB::table('goods')->find($request->id);
        // 商品所属分类
        $class=DB::table('goodscategory')->find($piece->type);
        // 商品图片
        $imgs=DB::table('goodsimg')->where('gid',$request->id)->get();
        // 商品尺码
        $size=DB::table('goodssize')->where('gid',$request->id)->distinct()->pluck('size');
        // 库存
        $stockall=DB::table('goodssize')->where('gid',$request->id)->sum('stock');
        // 评价
        $comment=DB::table('goodscomment')->join('goodssize','goodssize.id','=','goodscomment.sid')->join('vip','vip.id','=','goodscomment.uid')->where('gid',$request->id)->orderBy('ctime','desc')->paginate(10);
        // 评价附图
        foreach($comment as $i=>$clist){
            $cimgs[$i]=explode('&&',$clist->imgs);
        }
        // 热卖商品
        $hotgoods=DB::table('goods')->orderBy('sales','desc')->limit(10)->get();
        // 相关推荐
        $corgoods=DB::table('goods')->where('keywords','like',"%{$class->classify}%")->limit(5)->get();
        // 最近浏览
        $recgoods=DB::table('goods')->whereIn('id',$_SESSION['history'])->limit(5)->get();
        if(isset($cimgs)){
            return view('index/webpage/item_show',['piece'=>$piece,'class'=>$class->classify,'imgs'=>$imgs,'size'=>$size,'stockall'=>$stockall,'comment'=>$comment,'cimgs'=>$cimgs,'shopcart_count'=>$shopcart_count,'hotgoods'=>$hotgoods,'corgoods'=>$corgoods,'recgoods'=>$recgoods]);
        }else{
            return view('index/webpage/item_show',['piece'=>$piece,'class'=>$class->classify,'imgs'=>$imgs,'size'=>$size,'stockall'=>$stockall,'comment'=>$comment,'shopcart_count'=>$shopcart_count,'hotgoods'=>$hotgoods,'corgoods'=>$corgoods,'recgoods'=>$recgoods]);
        }
    }

    
}
